package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping
    public R saveStmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.saveStmeal(setmealDto);
        return R.success("保存套餐成功！！");
    }

    @GetMapping("/page")
    public R list(Integer page, Integer pageSize, String name) {
        IPage pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        setmealService.page(pageInfo, qw);
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal setmeal : records) {
            Long categoryId = setmeal.getCategoryId();
            LambdaQueryWrapper<Category> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(Category::getId, categoryId);
            Category category = categoryService.getOne(qw1);
            String categoryName = category.getName();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(categoryName);
            setmealDtos.add(setmealDto);
        }
        long total = pageInfo.getTotal();
        Map data = new HashMap();
        data.put("total",total);
        data.put("records",setmealDtos);

        return R.success(data);
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("套餐删除成功");
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("操作成功");
    }

    @Cacheable(value = "setmeal",key = "#id")
    @GetMapping("/{id}")
    public R getSetmeal(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.getSetmealDto(id);
        return R.success(setmealDto);
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateByIdWithSetmealDish(setmealDto);
        return R.success("修改套餐成功");
    }

    @Cacheable(value = "setmeal",key = "#categoryId+'::'+#status")
    @GetMapping("/list")
    public R getSetmealById(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }

    @Cacheable(value = "setmeal",key = "#id")
    @GetMapping("/dish/{id}")
    public R  dish(@PathVariable Long id){
        List list=setmealService.listDishes(id);
        return R.success(list);
    }

}
